<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<span id="s1"></span>
<button onclick="funA()">ajax发送消息</button>
</body>
</html>
<script>
    let num = Math.floor(Math.random() * 100 + 1);

    function funA() {
        // 1⃣️ 产生ajax对象
        let ajax = new XMLHttpRequest();
        // 2⃣️ 建立ajax和servlet之间的链接
        ajax.open('get', 'second');
        // 3⃣️ 发送
        ajax.send();
        // 4⃣️ 接收服务器的响应
        ajax.onreadystatechange = function () {
            if (ajax.readyState === 4) {
                // 获取服务器的响应
                let result = ajax.responseText;
                console.log('服务器的响应是：' + result);
            }
        }
    }

    window.onload = function () {
        document.getElementById('s1').innerText = num;
    }
</script>